home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Crash & Burn / source code / (Internalized Files) / Development / Libraries / Hubauer / RectUtilities.cp < prev    next >
Text File  |  1995-03-14  |  2KB  |  100 lines

  1. #include "RectUtilities.h"
  2.  
  3. #define f(a)    ((float)(a))
  4.  
  5. void    RectUtil::ScaleRectToFit(Rect*    source,Rect*    dest)
  6. {
  7.     long    destWidth = RectWidth(*dest);
  8.     long    destHeight = RectHeight(*dest);
  9.     long    sourceWidth = RectWidth(*source);
  10.     long    sourceHeight = RectHeight(*source);
  11.  
  12.     
  13.     if(sourceWidth > destWidth){
  14.         long    newHeight = (f(sourceHeight)/f(sourceWidth)) * f(destWidth);
  15.         source->bottom = source->top + newHeight;
  16.         source->right = source->left + destWidth;
  17.     }
  18.     
  19.     sourceWidth = RectWidth(*source);
  20.     sourceHeight = RectHeight(*source);
  21.     
  22.     if(sourceHeight > destHeight){
  23.         long    newWidth = (f(sourceWidth)/f(sourceHeight)) * f(destHeight);
  24.         source->bottom = source->top + destHeight;
  25.         source->right = source->left + newWidth;
  26.     }
  27.         
  28.         
  29. }
  30.  
  31.  
  32. void    RectUtil::ScaleRectToContain(Rect* enclRect,Rect* objRect)
  33. {
  34.     long        enclWidth = RectWidth(*enclRect);
  35.     long        enclHeight = RectHeight(*enclRect);
  36.     long        objWidth = RectWidth(*objRect);
  37.     long        objHeight = RectHeight(*objRect);
  38.     
  39.     
  40.     if(enclWidth < objWidth){
  41.         long    newEnclHeight = (f(enclHeight) / f(enclWidth)) * f(objWidth);
  42.         enclRect->bottom = enclRect->top + newEnclHeight;
  43.         enclRect->right = enclRect->left + objWidth;
  44.     }
  45.     
  46.     enclWidth = RectWidth(*enclRect);
  47.     enclHeight = RectHeight(*enclRect);
  48.  
  49.     if(enclHeight < objHeight){
  50.         long     newEnclWidth = (f(enclWidth) / f(enclHeight)) * f(objHeight);
  51.         enclRect->bottom = enclRect->top + objHeight;
  52.         enclRect->right = enclRect->left + newEnclWidth;
  53.     }
  54. }
  55.  
  56.  
  57.  
  58. void    RectUtil::CenterRectToFit(Rect*    source,Rect*    dest)
  59. {
  60.     Point        center;
  61.     
  62.     center.h = dest->left + RectWidth(*dest)/2;
  63.     center.v = dest->top + RectHeight(*dest)/2;
  64.     
  65.     short    sourceHeight = RectHeight(*source);
  66.     short    sourceWidth = RectWidth(*source);
  67.     
  68.     source->top = center.v - sourceHeight/2;
  69.     source->bottom = source->top + sourceHeight;
  70.     
  71.     source->left = center.h - sourceWidth/2;
  72.     source->right = source->left + sourceWidth;
  73. }
  74.  
  75.  
  76. void    RectUtil::CenterRectOnPoint(Rect* theRect,Point center)
  77. {
  78.     short    width = RectWidth(*theRect);
  79.     short    height = RectHeight(*theRect);
  80.     
  81.     theRect->top = center.v - height/2;
  82.     theRect->bottom = theRect->top + height;
  83.     theRect->left = center.h - width/2;
  84.     theRect->right = theRect->left + width;
  85. }
  86.  
  87.  
  88.  
  89. void    RectUtil::LocalToGlobal(Rect* theRect)
  90. {
  91.     ::LocalToGlobal(&topLeft(*theRect));
  92.     ::LocalToGlobal(&botRight(*theRect));
  93. }
  94.  
  95. void RectUtil::GlobalToLocal(Rect* theRect)
  96. {
  97.     ::GlobalToLocal(&topLeft(*theRect));
  98.     ::GlobalToLocal(&botRight(*theRect));
  99. }
  100.